﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace SPVisualDev.VSAddin.Dialogs
{
    public partial class DialogError : Form
    {
        public DialogError()
        {
            InitializeComponent();

            this.Load += new EventHandler(DialogError_Load);
            this.Activated += new EventHandler(DialogError_Activated);
        }

        private void DialogError_Load(object sender, EventArgs e)
        {
            try
            {
                SizeF size = this.txtMessage.CreateGraphics().MeasureString(this.ErrorMessage, this.txtMessage.Font, this.txtMessage.Width);
                int height = Convert.ToInt32(size.Height);

                this.txtMessage.MaximumSize = new Size(this.txtMessage.Width, 64);
                this.txtMessage.Height = height + 4;

                if (height > 63)
                    this.txtMessage.ScrollBars = ScrollBars.Vertical;

                this.lblExceptionInfo.Top = this.txtMessage.Height + this.txtMessage.Top + 6;
                this.txtExceptionInfo.Top = this.lblExceptionInfo.Top + this.lblExceptionInfo.Height + 6;
                this.txtExceptionInfo.Size = new Size(this.txtExceptionInfo.Width, 80);


                this.Size = new Size(this.ClientSize.Width, this.txtExceptionInfo.Top + this.txtExceptionInfo.Height + this.btnOK.Height + 46);
                this.MinimumSize = new Size(this.Width, this.Height);

                this.btnOK.Top = this.ClientSize.Height - 36;
                this.btnCancel.Top = this.ClientSize.Height - 36;

                this.btnOK.Focus();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error occured: " + ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void DialogError_Activated(object sender, EventArgs e)
        {
            try
            {
                this.txtMessage.DeselectAll();
                this.txtExceptionInfo.DeselectAll();

                this.btnOK.Focus();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error occured: " + ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public string ErrorMessage
        {
            get { return txtMessage.Text; }
            set { txtMessage.Text = value; }
        }

        public string ExceptionInfo
        {
            get { return txtExceptionInfo.Text; }
            set { txtExceptionInfo.Text = value; }
        }

        public bool ShowCancelButton
        {
            set { btnCancel.Visible = value; }
        }

        public string AcceptButtonText
        {
            set { btnOK.Text = value; }
        }

        public static DialogResult ShowError(Exception exception)
        {
            return ShowError(exception, null, false);
        }

        public static DialogResult ShowError(Exception exception, bool ShowCancelButton)
        {
            return ShowError(exception, null, ShowCancelButton);
        }

        public static DialogResult ShowError(Exception exception, string AdditionalText, bool ShowCancelButton)
        {
            return ShowError(exception, AdditionalText, "OK", ShowCancelButton);
        }

        public static DialogResult ShowError(Exception exception, string AdditionalText, string OKButtonText, bool ShowCancelButton)
        {
            try
            {
                using (DialogError dialog = new DialogError())
                {
                    dialog.ErrorMessage = (string.IsNullOrEmpty(AdditionalText)) ? exception.Message : AdditionalText + " " + exception.Message;
                    dialog.btnOK.Text = OKButtonText;

                    string exceptionInfo = exception.GetType().FullName;

                    string stackTrace;
                    if (string.IsNullOrEmpty(exception.StackTrace))
                        stackTrace = exception.InnerException.StackTrace;
                    else
                        stackTrace = exception.StackTrace;

                    dialog.ExceptionInfo = exceptionInfo + " " + stackTrace;
                    dialog.ShowCancelButton = ShowCancelButton;

                    return dialog.ShowDialog();
                }

            }
            catch (Exception ex)
            {
                return MessageBox.Show("Error occured: " + ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                
            }
        }
    }
}
